import { getPermissionListApi } from '@/services/api';
import { useRequest } from 'ahooks';
import { Tree } from 'antd';
import { useMemo } from 'react';

const AuthTreeFormItem = ({ onChange, value }: any) => {
  const { data } = useRequest(async () => {
    const _res = await getPermissionListApi();

    return _res?.data;
  });

  console.log('data', data);

  // 换电功能权限列表
  const huandian = useMemo(() => {
    return data?.filter((e) => {
      return [1, 2, 3].includes(e?.code);
    });
  }, [data]);

  // 换电数据
  const huandianData = useMemo(() => {
    return data?.filter((e) => {
      return [4, 5, 6].includes(e?.code);
    });
  }, [data]);

  return (
    <Tree
      checkable
      //   defaultExpandedKeys={[1]}
      //   defaultSelectedKeys={[1]}
      defaultCheckedKeys={value}
      checkedKeys={value}
      onCheck={(keys, { checkedNodes }) => {
        onChange?.(
          checkedNodes
            ?.filter((_data) => {
              return !_data?.children;
            })
            ?.map((e) => e.key),
        );
      }}
      blockNode
      defaultExpandAll
      treeData={[
        {
          title: '全部权限',
          key: '0',
          children: data?.map((e) => {
            return {
              title: e?.title,
              key: e?.id,
            };
          }),
        },
      ]}
    />
  );
};

export default AuthTreeFormItem;
